//KeyInfo.java
package com.dp.service.impl3;

import java.sql.SQLException;


import com.dp.pub.MyServer;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright:
 * <p>Company: </p>
 * @author xiaobin
 * @version 1.0
 */
public class KeyInfo {
    private long keyMax;
    private long keyMin;
    private long nextKey;
    private long poolSize;
    private String keyName;

    /**
     *������
    */
    public KeyInfo(long poolSize, String keyName) {
        this.poolSize = poolSize;
        this.keyName = keyName;
        retrieveFromDB();
    }

    /**
     *ȡֵ�������ṩ������ֵ
     */
    public long getKeyMax() {
        return keyMax;
    }

    /**
     *ȡֵ�������ṩ�����Сֵ
     */
    public long getKeyMin() {
        return keyMin;
    }

    /**
     *ȡֵ�������ṩ��ĵ�ǰֵ
     */
    public long getNextKey() {
        if (nextKey > keyMax) {
            retrieveFromDB();
        }

        return nextKey++;
    }

    /**
     *�ڲ�����������ݿ���ȡ��ĵ�ǰֵ
     */
    private void retrieveFromDB() {
        String selectSql = "select key_value from main_key where key_name = '" +
            keyName + "'";

    	ValidBeanImpl v=(ValidBeanImpl) MyServer.ctx.getBean("validBeanImpl");
    	 try {
    		 String temp=v.getOneString(selectSql);
        boolean isExist = !"".equals(temp);

        
      //  DBOperate conn = new DBOperate();
        String[] batchSql;

       
            if (isExist) {
                keyMin = Long.parseLong(temp);//this.distillName("key_value", selectSql));
                keyMax = (keyMin + poolSize) - 1;
                nextKey = keyMin;

               
                batchSql = new String[1];
            } else {
                keyMin = 1;
                keyMax = (keyMin + poolSize) - 1;
                nextKey = keyMin;
                batchSql = new String[2];

                batchSql[0] = "INSERT INTO main_key (key_name,key_value) VALUES('" +
                    keyName + "',1)";

                
            }

            if (batchSql.length == 2) {
                batchSql[1] = "update main_key set key_value=" + (keyMax + 1) +
                    " where key_name='" + keyName + "'";

                
            } else {
                batchSql[0] = "update main_key set key_value=" + (keyMax + 1) +
                    " where key_name='" + keyName + "'";

                
            }
        
            v.execTransaction(batchSql);
        } catch (Exception e) {
        	 e.printStackTrace();
        }
    }

//    public boolean hasRecord(String sqlStatement) {
//        boolean hasRecord = false;
//
//        DBOperate conn = new DBOperate();
//
//        try {
//            java.util.HashMap hashMap = conn.queryOneRow(sqlStatement);
//
//            if (hashMap.size() >= 1) {
//                hasRecord = true;
//            }
//        } catch (Exception e) {
//        }
//
//        return hasRecord;
//    }

//    public String distillName(String value, String sqlStatement)
//        throws SQLException {
//        String str = "";
//        DBOperate conn = new DBOperate();
//        java.util.HashMap hashMap = null;
//
//        try {
//            hashMap = conn.queryOneRow(sqlStatement);
//            str = this.getString(hashMap.get("key_value"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return str;
//    }

    public String getString(Object object) {
        try {
            return object.toString();
        } catch (Exception e) {
        	e.printStackTrace();
            return "1";
        }
    }
}
